$(function() {
	mui.init();
	//请求JSON
	var req = {
		'caseid': localStorage.getItem('caseid'),
		'userid': localStorage.getItem('userid'),
		'token': localStorage.getItem('token')
	};
	//相册预览
	mui.previewImage();
	mui.ready(function() {
		//加载框
		mui.showLoading("正在加载..", ".warn-list");
		Request();
		// 费用详情表格弹出
		mui('.repair-btn').on('tap', 'span', function() {
			$('.fee-detail').fadeIn();
			fei();
		});
		//点击空白区域收起
		mui('body').on('tap', '.fee-detail', function(e) {
			var _con = $('.fee-detail-box'); // 设置目标区域
			if (!_con.is(e.target) && _con.has(e.target).length === 0) {
				$('.fee-detail').fadeOut();
			}
		})
		//工作定位地图
		mui('.repair-loc').on('tap', '.flowbtn', function() {
			mui.openWindow({
				url: 'urgent-table-repair-map.html',
				id: 'urgent-table-repair-map.html'
			})
		});
	});
	//ajax请求处理数据
	function Request() {
		var url = domain + "/dtwbgl/jjoverdesc.h5";
		$.ajax(url, {
			// data: JSON.stringify(req),
			data: req,
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型
			timeout: 30000, //超时时间
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
			},
			async: true, //true异步加载
			success: function(data) {
				if (data.code == 2001) {
					//返回值处理
					var ref = data.data;
					/** 数据处理 */
					var caseid = ref.caseid; //工单编号
					var gztypecn = ref.gztypecn; //故障原因
					var dtdizhi = ref.dtdz; //电梯位置
					var pftime = timeSub(ref.xdtime); // 派发时间
					var jstime = timeSub(ref.jdtime); // 接收时间
					var tjtime = timeSub(ref.tjtime); // 提交时间
					var jobtime = ''; //工作时间
					var wbpeople = ref.wbmobile; // 维保人员（数组）
					var pj = ref.pj; //评价  （数组）
					var guzhangdesr = ref.guzhangdesr;
					var company=ref.wbname;//公司名称
					$('.company').html(company); //公司名称
					$('.repair-detail-txt .txtbox span.blue').html(caseid); //工单编号
					$('.repair-detail-txt .txtbox b').html(gztypecn); //工单状态
					$('.repair-detail-txt .txtbox span.dz').html(dtdizhi); //电梯位置
					$('.chul p').html(guzhangdesr); //晋级描述
					if (pftime != '') { // 派发时间
						$('.repair-flow>div:first-child p').html(pftime);
					} else {
						$('.repair-flow>div:first-child').addClass('active');
					}
					if (jstime != '') { // 接收时间
						$('.repair-flow>div:nth-child(2) p').html(jstime);
					} else {
						$('.repair-flow>div:nth-child(2)').addClass('active');
					}
					if (tjtime != '') { // 提交时间
						$('.repair-flow>div:last-child p').html(tjtime);
						//  工作时间
						//  工作时间				
						var date1 = new Date(jstime.replace(/-/g, "/"));
						var date2 = new Date(tjtime.replace(/-/g, "/"));
						var s1 = date1.getTime(),
							s2 = date2.getTime();
						var total = (s2 - s1) / 1000;
						var day = parseInt(total / (24 * 60 * 60)); //计算整数天数
						var afterDay = total - day * 24 * 60 * 60; //取得算出天数后剩余的秒数
						var hour = parseInt(afterDay / (60 * 60)); //计算整数小时数
						var afterHour = total - day * 24 * 60 * 60 - hour * 60 * 60; //取得算出小时数后剩余的秒数
						var min = parseInt(afterHour / 60); //计算整数分
						var afterMin = total - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60; //取得算出分后剩余的秒数
						// return day + "天" + hour + "时" + min + "分" + afterMin + "秒";
						jobtime = day + "天" +hour + "小时 " + min + " 分钟";
						$('.job-qingkuang .monbox p:first-child b').html(jobtime);
						$('.job-qingkuang .monbox p:last-child b').html(tjtime);

					} else {
						$('.repair-flow>div:last-child').addClass('active');
					}
					//判断是否有维保人员
					var rylen = wbpeople.length;
					var ryhtml = '';
					if (rylen != 0) {
						//添加维保人员
						for (var i = 0; i < rylen; i++) {
							ryhtml +=
								`
                            <p> ${wbpeople[i].truename}:<span>${wbpeople[i].mobile}</span> </p>
                            `;
						}
						$('.job-people').append(ryhtml);
					}
					//判断是否有现场图片
					var ph = ref.wbpiclist;
					var imlen = ph.length;
					var imhtml = '';
					console.log(ph);
					if (imlen != 0) {
						//添加图片
						ph.forEach(function(e, k) {
							imhtml +=
								`
									<li>
										<span>
											<img src="${e.dbpath}" data-preview-src="${e.dbpath}"
											data-preview-group="1">
										</span>
									</li>
									`;
						})
						//                         for (var i = 0; i < imlen; i++) {
						//                             
						//                         }
						$('.repair_img ul').append(imhtml);
					}
					//判断是否有上报图片
					var phs = ref.wypiclist;
					var imlens = ph.length;
					var imhtmls = '';

					var wyurl = "http://"+configureIp + '/upimg/';

					if (imlens != 0) {
						//添加图片
						phs.forEach(function(e, k) {
							var imgUrl = wyurl + e.url
							console.log(imgUrl);
							imhtmls +=
								`
									<li>
										<span>
											<img src= "${imgUrl}" data-preview-src="${imgUrl}"
											data-preview-group="2">
										</span>
									</li>
									`;
						})

						$('.shang_img ul').append(imhtmls);
					}
					//维保评价
					var zl = parseInt(ref.smile); //到达时效
					var td = parseInt(ref.finger); //急修满意度
					for (n = 0; n < zl; n++) {
						$('.review-star i').eq(n).addClass('on');
					}
					for (n = 0; n < td; n++) {
						$('.review-heart i').eq(n).addClass('on');
					}


				} else if (data.code == 2002) {
					mui.alert(data.msg);
					mui.openWindow({
						url: '../../login.html',
						id: 'login.html'
					})
					console.log(data);
				} else {
					mui.alert(data.msg);
					mui.openWindow({
						url: '../../404.html',
						id: '404.html'
					})
				}
				//隐藏加载框
				mui.hideLoading(function() { //隐藏后的回调函数  
				});
			},
			error: function(xhr, type, errorThrown) { //异常处理； console.log(type);
				mui.alert(type);
				mui.openWindow({
					url: '../../login.html',
					id: 'login.html'
				})
			}
		});
	}

	// 费用加载
	function fei() {
		var url = domain + "/dtwbgl/jjoverprice.h5";
		$.ajax(url, {
			//data: JSON.stringify(req),
			data: req,
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型
			timeout: 30000, //超时时间
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
			},
			async: true, //true异步加载
			success: function(ref) {
				if (ref.code == 2001) {
					//配件(数组)
					console.log(ref);

					var peijian = ref.data.pjlist;
					var mfhtml = ''; //免费项
					var sfhtml = ''; //收费项
					var feeTotal = 0; //总价                 
					var peilen = peijian.length;
					for (var x = 0; x < peilen; x++) {
						if (peijian[x].realprice > 0) {
							sfhtml +=
								`
								<div class="mui-table fee-tab-body unfree">
									<div class="mui-table-cell mui-text-center">
										<p>${peijian[x].name}</p>
									</div>
									<div class="mui-table-cell mui-text-center">
										<p>${peijian[x].pjtype}</p>
									</div>
									<div class="mui-table-cell mui-text-center">
										<p>${peijian[x].dw}</p>
									</div>
									<div class="mui-table-cell mui-text-center">
										<p>${peijian[x].count}</p>
									</div>
									<div class="mui-table-cell mui-text-center">
										<p>${peijian[x].realprice}</p>
									</div>
								</div>
								`;
							feeTotal = feeTotal + peijian[x].count * peijian[x].realprice;
						} else {
							mfhtml +=
								`
								<div class="mui-table fee-tab-body free">
									<div class="mui-table-cell mui-text-center">
										<p>${peijian[x].name}</p>
									</div>
									<div class="mui-table-cell mui-text-center">
										<p>${peijian[x].pjtype}</p>
									</div>
									<div class="mui-table-cell mui-text-center">
										<p>${peijian[x].dw}</p>
									</div>
									<div class="mui-table-cell mui-text-center">
										<p>${peijian[x].count}</p>
									</div>
									<div class="mui-table-cell mui-text-center">
										<p>${peijian[x].realprice}</p>
									</div>
								</div>
								`;
						}
					}
					$('.fee-detail-box').find('.unfree,.free').remove();
					$('.fee-mf').append(mfhtml);
					$('.fee-sf').append(sfhtml);
					//总价
					$('.fee-total').html(feeTotal);

				} else if (ref.code == 2002) {
					mui.toast(ref.msg);
					mui.openWindow({
						url: 'login.html',
						id: 'login.html'
					})
				} else {
					mui.toast(ref.msg);
					mui.openWindow({
						url: '404.html',
						id: '404.html'
					})
				}
			},
			error: function(xhr, type, errorThrown) { //异常处理； console.log(type);
				mui.alert(type);
				mui.openWindow({
					url: '../../login.html',
					id: 'login.html'
				})
			}
		})
	}
	//时间计算
    function timeCal(time1, time2) {
        var date1 = new Date(time1.replace(/-/g, "/"));
        var date2 = new Date(time2.replace(/-/g, "/"));
        var s1 = date1.getTime(),
            s2 = date2.getTime();
        var total = (s2 - s1) / 1000;
        var day = parseInt(total / (24 * 60 * 60)); //计算整数天数
        var afterDay = total - day * 24 * 60 * 60; //取得算出天数后剩余的秒数
        var hour = parseInt(afterDay / (60 * 60)); //计算整数小时数
        var afterHour = total - day * 24 * 60 * 60 - hour * 60 * 60; //取得算出小时数后剩余的秒数
        var min = parseInt(afterHour / 60); //计算整数分
        var afterMin = total - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60; //取得算出分后剩余的秒数
        return day + "天" + hour + "时" + min + "分" + afterMin + "秒";
    }
    function timeSub(str) {
        var endIndex = str.length - 3;
        var time = str.substring(0, endIndex)
        return time;
    }
})
